package com.lanju.service.impl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.lanju.common.WxMessageUtil;
import com.lanju.dao.UserDao;
import com.lanju.dao.ZuKeContractDao;
import com.lanju.entity.User;
import com.lanju.entity.ZukeContract;
import com.lanju.service.ZuKeContractService;
@Service
public class ZuKeContractServiceImpl implements ZuKeContractService {
	
	@Autowired
	private ZuKeContractDao zuKeContractDao;
	@Autowired 
	private UserDao userDao;
	
	
	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> createContract(ZukeContract contract) {
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			zuKeContractDao.saveContract(contract);
			result.put("success", true);
			result.put("msg", "创建成功");
			result.put("contractId", contract.getContract_id());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "创建失败");
		}		
		return result;
	}
	
	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> updateConstract(ZukeContract contract) {
		Map<String,Object> result=new HashMap<>();
		try {
			zuKeContractDao.updateConstract(contract);
			result.put("success", true);
			result.put("msg", "修改成功");
			result.put("contractId", contract.getContract_id());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "修改失败");
		}
		return result;
	}

	@Override
	public Map<String, Object> getConstractDetailById(Map<String, Object> map) {
		Map<String,Object> result=new HashMap<>();
		try {
			ZukeContract contract=zuKeContractDao.getContract(map);
			result.put("contractDetail", contract);
			result.put("success", true);
			result.put("msg", "查询成功");
			result.put("flag", map.get("flag").toString());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("rows", null);
			result.put("success", false);
			result.put("msg", "查询失败");
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> delContract(Map<String, Object> map) {
		Map<String,Object> result=new HashMap<>();
		try {
			ZukeContract contract =zuKeContractDao.getContract(map);
			if(contract!=null){
				zuKeContractDao.delContract(contract);
				result.put("success", true);
				result.put("msg", "删除成功");
			}else{
				result.put("success", false);
				result.put("msg", "合同不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "操作出错");
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getContractList(Map<String, Object> map) {
		Map result=new HashMap<>();
		try {
			result.put("rows", zuKeContractDao.getContractList(map));
			result.put("flag", map.get("flag").toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> signComtract(Map<String, Object> map) {
		Map<String,Object> result=new HashMap<String,Object>();
		try {
			ZukeContract contract=zuKeContractDao.getContract(map);
			if(contract !=null){
				contract.setStatus(1);
				zuKeContractDao.signComtract(contract);
				result.put("success", true);
				result.put("msg", "签名成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "签名失败");
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> updatePayStatus(Map<String, Object> map,HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> result=new HashMap<String,Object>();
		try {
			ZukeContract contract=zuKeContractDao.getContract(map);
			User u = userDao.getById(contract.getUserId());
			map.put("reviceType", "支付管理员");
			List<User> ulist=userDao.getSendMessageUser(map);
			System.out.println("总共查询到"+ulist+"位管理员");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if(contract !=null){
				contract.setPayStatus(1);
				zuKeContractDao.signComtract(contract);
				result.put("success", true);
				result.put("msg", "支付审核中");
				String templateId  ="x1XmbKmGDV_1Hd3bSCiheu1EiS86OxQLdRT5_XPu6zM";
				Integer count=0;
				if(ulist != null && ulist.size()>0){
					for(int i=0;i<ulist.size();i++){
						User u_=ulist.get(i);
						if(!StringUtils.isEmpty(u_.getOpenId())){
							System.out.println((Integer.valueOf(contract.getRental_price())+Integer.valueOf(contract.getTotal_cash())));
							String data="{\"touser\":\""+u_.getOpenId()+"\",\"template_id\":\""+templateId+"\",\"topcolor\":\"#FF0000\", "
									+ "\"data\":{\"first\": { \"value\":\""+"租客缴纳租金和押金消息提醒"+"\",\"color\":\"#173177\"},"
											+ "\"keyword1\":{\"value\":\""+(u.getRealName()==null?u.getNickName():u.getRealName())+"\",\"color\":\"#173177\" },"
													+ "\"keyword2\": { \"value\":\""+contract.getRenter_addr()+"\",\"color\":\"#173177\"},"
															+ "\"keyword3\":{\"value\":\""+(sdf.format(contract.getRenter_start_time()) +"~"+sdf.format(contract.getRenter_end_time()))+"\",\"color\":\"#173177\"}, "
															+ "\"keyword4\":{\"value\":\""+("￥ "+(Integer.valueOf(contract.getRental_price())+Integer.valueOf(contract.getTotal_cash())))+"\",\"color\":\"#173177\"}, "
															+ "\"keyword5\":{\"value\":\""+"不详"+"\",\"color\":\"#173177\"}, "
																	+ "\"remark\":{\"value\":\""+"请尽快在后台处理"+"\",\"color\":\"#173177\"}"
																			+ "}}";
							synchronized (data) {
								WxMessageUtil.sendcontractPayMessage(request, response, data);
								count ++;
							}
						}	
					}
				}

				System.out.println("总共发送给"+count+"个管理员");
				result.put("sendinfo", "总共发送给"+count+"个管理员");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "支付失败");
		}
		return result;
	}

}
